package com.hbyj.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Struct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hwpf.extractor.WordExtractor;



import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.M_set;
import com.hbyj.hbm.vo.MrDepartment;



/**
 * 常用函数
 * @author lc
 *
 */
public class ChangeTool {
	private static double maxValue = 100000000000.00;
	/**
	 * @param areaCode 进行行政区划编码转换
	 * @return 
	 */
	public static String ChangeArea(String areaCode){
		String code = "000000";
		if(areaCode.length()==6){
			if(areaCode.substring(2, 6).equals("0000")){
				code = areaCode.substring(0, 2);
			}else if(areaCode.substring(4,6).equals("00")){
				code = areaCode.substring(0, 4);
			}else{
				code = areaCode;
			}
		}
		return code;
	}

	public static   String getFinalValue(String value){
		String areaName = "湖北省";
		String sql = "select AreaName from Area where AreaCode = '"+value+"'";
		List list = null;
		DbTools db = new DbTools();
		list = db.findBySql(sql);
		areaName = list.size()>0?list.get(0).toString():areaName;
		return areaName;
	} 
	
	/**
	 * 获取下拉数据
	 * @param resIt
	 * @return
	 */
	public static String getProName(Iterator<Object> resIt){
		String strResult="";
		Object obj=null;
		int num = 0;
		while(resIt.hasNext()){
			obj=(Object)resIt.next();
			strResult+=obj.toString()+"$";
			num++;
			if(num>9)
				break;
		}
		return strResult;
	}
	
    /**
     * 去除数组中重复数据并对其进行排序
     * @param a
     * @return
     * @throws Exception 
     */
    public static String[] RemoveRepeat(String[] a) throws Exception {
        List<String> list = new LinkedList<String>();
        for(int i = 0; i < a.length; i++) {
            if(!list.contains(a[i])) {
                list.add(a[i]);
            }
        }
        String[] date = (String[])list.toArray(new String[list.size()]);
        Long[] l = new Long[date.length]; 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i = 0;i<l.length;i++){
        	l[i] = sdf.parse(date[i]).getTime();
        }
        Arrays.sort(l);
        String[] res = new String[l.length];
        for(int i = 0;i<l.length;i++){
        	Date d = new Date(l[i]);
        	res[i] = sdf.format(d);
        }
        return res;
    }
    
    public static boolean isExit(List list,String res){
    	boolean isexit = false;
    	for(int i = 0;i<list.size();i++){
    		String str = (String)list.get(i);   
    		if(str.equals(res)){
    			isexit = true;
    		}
    	}
    	return isexit;
    }
    public static Date getModifiedTime(String path) throws ParseException{   
        File f = new File(path);               
        Calendar cal = Calendar.getInstance();   
        long time = f.lastModified();   
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          
        cal.setTimeInMillis(time);     
        return formatter.parse(formatter.format(cal.getTime()));
    }   
    
    public static String getZHType(String type){
    	String zhName = "";
    	if(type.equals("00")){
    		zhName = "斜坡变形";
    	}else if(type.equals("01")){
    		zhName = "滑坡";
    	}else if(type.equals("02")){
    		zhName = "崩塌";
    	}else if(type.equals("03")){
    		zhName = "泥石流";
    	}else if(type.equals("04")){
    		zhName = "岩溶塌陷";
    	}else if(type.equals("05")){
    		zhName = "采空塌陷";
    	}else if(type.equals("06")){
    		zhName = "地裂缝";
    	}else if(type.equals("07")){
    		zhName = "地面沉降";
    	}else if(type.equals("08")){
    		zhName = "塌岸";
    	}else if(type.equals("09")){
    		zhName = "高切坡";
    	}else if(type.equals("10")){
    		zhName = "危岩体";
    	}
    	return zhName;
    }
  
    
    public static String utf8Togb2312(String str){
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            switch (c) {
               case '+':
                   sb.append(' ');
               break;
               case '%':
                   try {
                        sb.append((char)Integer.parseInt(
                        str.substring(i+1,i+3),16));
                   }
                   catch (NumberFormatException e) {
                       throw new IllegalArgumentException();
                  }
                  i += 2;
                  break;
               default:
                  sb.append(c);
                  break;
             }
        }
        // Undo conversion to external encoding
        String result = sb.toString();
        String res=null;
        try{
            byte[] inputBytes = result.getBytes("8859_1");
            res= new String(inputBytes,"UTF-8");
        }
        catch(Exception e){}
        return res;
  }  
    public static String string2Json(String s) { 
        StringBuilder sb = new StringBuilder(s.length()+20); 
        sb.append('\"'); 
        for (int i=0; i<s.length(); i++) { 
            char c = s.charAt(i); 
            switch (c) { 
            case '\"': 
                sb.append("\\\""); 
                break; 
            case '\\': 
                sb.append("\\\\"); 
                break; 
            case '/': 
                sb.append("\\/"); 
                break; 
            case '\b': 
                sb.append("\\b"); 
                break; 
            case '\f': 
                sb.append("\\f"); 
                break; 
            case '\n': 
                sb.append("\\n"); 
                break; 
            case '\r': 
                sb.append("\\r"); 
                break; 
            case '\t': 
                sb.append("\\t"); 
                break; 
            default: 
                sb.append(c); 
            } 
        } 
        sb.append('\"'); 
        return sb.toString(); 
     } 
    public static String[] getBetweenTime(){
		String[] tempString  = new String[2];
		Calendar  cd  =  Calendar.getInstance(); 
	
        String   lastDay   =  String.valueOf(cd.get(Calendar.DAY_OF_MONTH));
        
        String year = String.valueOf(cd.get(Calendar.YEAR));
        String month = String.valueOf(cd.get(Calendar.MONTH)+1);
        
        if(Integer.parseInt(month, 10)<10) month="0"+month;
        if(Integer.parseInt(lastDay, 10)<10) lastDay="0"+lastDay;
        tempString  = new String[]{year+"-"+month+"-01", year+"-"+month+"-"+lastDay};
		return tempString;	                          
	}
    
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 根据灾害类型和规模级别判定规模的最大值和最小值
     * @param zhdType
     * @param level
     * @return
     */
    public static  M_set getGMByLevel(String zhdType,String level){
    	
    	switch(Integer.parseInt(zhdType)){
    	case 1: return getHPValue(level);//滑坡
    	case 2: return getBTValue(level);//崩塌
    	case 3: return getNSLValue(level);//泥石流
    	case 4: return getTXValue(level);//岩溶塌陷
    	case 6: return getDLFValue(level);//地裂缝
    	default: return getDMCJForAreaValue(level);//地面沉降
    	}
    	
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 滑坡规模判定
     * @param level
     * @return
     */
    private static  M_set getHPValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = maxValue;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 1000;break;
    	case 大型:
    		min = 100;
    		max = 1000;break;
    	case 中型:
    		min = 10;
    		max = 100;break;
    	case 小型:
    		min = 0;
    		max = 10;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 崩塌规模判定
     * @param level
     * @return
     */
    private static  M_set getBTValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = maxValue;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 1000;break;
    	case 大型:
    		min = 10;
    		max = 100;break;
    	case 中型:
    		min = 1;
    		max = 10;break;
    	case 小型:
    		min = 0;
    		max = 1;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 泥石流规模判定
     * @param level
     * @return
     */
    private static  M_set getNSLValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = maxValue;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 1000;break;
    	case 大型:
    		min = 10;
    		max = 100;break;
    	case 中型:
    		min = 1;
    		max = 10;break;
    	case 小型:
    		min = 0;
    		max = 1;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 
     * @param level
     * @return
     */
    private static  M_set getTXValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = maxValue;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 20;break;
    	case 大型:
    		min = 10;
    		max = 20;break;
    	case 中型:
    		min = 1;
    		max = 10;break;
    	case 小型:
    		min = 0;
    		max = 1;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 地裂缝规模判定
     * @param level
     * @return
     */
    private static  M_set getDLFValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = maxValue;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 10;break;
    	case 大型:
    		min = 5;
    		max = 10;break;
    	case 中型:
    		min = 1;
    		max = 5;break;
    	case 小型:
    		min = 0;
    		max = 1;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 地面沉降根据沉降面积判定规模
     * @param level
     * @return
     */
    private static  M_set getDMCJForAreaValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = maxValue;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 500;break;
    	case 大型:
    		min = 100;
    		max = 500;break;
    	case 中型:
    		min = 10;
    		max = 100;break;
    	case 小型:
    		min = 0;
    		max = 10;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    /**
     * author cuiyi
     * time 2012-8-15
     * disc 地面沉降根据最大累计沉降量判定规模
     * @param level
     * @return
     */
    private static  M_set getDMCJForStatValue(String level){
    	M_set ms = new M_set();
    	double min = 0;
    	double max = 2;
    	switch(levelType.valueOf(level)){
    	case 特大型:
    		min = 1;break;
    	case 大型:
    		min = 0.5;
    		max = 1;break;
    	case 中型:
    		min = 0.1;
    		max = 0.5;break;
    	case 小型:
    		min = 0;
    		max = 0.1;break;
    	}
    	ms.setMax_v(max);
    	ms.setMin_v(min);
    	return ms;
    }
    public static void main(String[] args){
    	//getBetweenTime();
    	/*String s = "2001";
    	String e = "2010";
    	int l = Integer.parseInt(s)-Integer.parseInt(e);
    	int[] years = new int[l];*/
    	 String filePath = "D:\\420100000033.txt";
         readTxtFile(filePath);
    }
    public enum levelType {  
    	  特大型,大型,中型,小型;
    } 
    /**
	 * 根据路径获取word的输出
	 * @throws Exception 
	 */
	public static String[] getWordContent(String patg){
		  FileInputStream in;
		  String[] content = null;
		try {
			in = new   FileInputStream(patg);
			 WordExtractor   extractor   =   new   WordExtractor(in); 
		   
			 String text = extractor.getText();
		     content = text.split("\\s{1,}") ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	        return content;
	}
	 public static String readTxtFile(String filePath){
		String lineTxt = "";
        try {
            String encoding="gbk";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
               
                String tmp = "";
                while((tmp = bufferedReader.readLine()) != null){
                	lineTxt +=tmp;
                    //System.out.println(lineTxt);
                }
                read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return lineTxt;
    }
	public static String getZhGMByCode(int code){
		String gm = "特大型";
		switch(code){
		case 1:
			gm = "特大型";
			break;
		case 2:
			gm = "大型";
			break;
		case 3:
			gm = "中型";
			break;
		case 4:
			gm = "小型";
			break;
		}
		return gm;
	}
	public static String getColorByCode(int code){
		String color = "FF0000";
		switch(code){
		case 1:
			color = "FF0000";
			break;
		case 2:
			color = "FFAD5B";
			break;
		case 3:
			color = "FFFF80";
			break;
		case 4:
			color = "0000FF";
			break;
		}
		return color;
	}
	public static String getJbqk01TypeByCode(int code){
		String type = "无类别点";
		switch(code){
		case 1:
			type = "十万调查点";
			break;
		case 2:
			type = "五万调查点";
			break;
		case 3:
			type = "应急调查点";
			break;
		case 4:
			type = "群测群防点";
			break;
		case 5:
			type = "重大灾害点";
			break;
		case 6:
			type = "应急监测点";
		}
		return type;
	}
	public static void createDoc() { 
		
    } 

}



